package com.iflysse.dao;

import com.iflysse.pojo.po.common.CommonPoResp;
import com.iflysse.pojo.po.req.*;
import com.iflysse.pojo.po.resp.LabelAllPoResp;
import com.iflysse.pojo.po.resp.LabelIdsPoResp;
import com.iflysse.pojo.po.resp.LabelPoResp;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

@Repository
public interface LabelDao {
    /**
     * 添加标签
     * @param addLabelPoReq
     * @return
     */
    @Insert({"insert into tb_label (label_id,label_title) values (UUID(),#{labelTitle})"})
    @Options(useGeneratedKeys = true, keyProperty = "label_id")
    int addLabel(AddLabelPoReq addLabelPoReq) throws SQLException;

    /**
     * 修改标签
     * @param labelPoReq
     * @return
     */
    @Update("update tb_label set label_title= #{labelTitle} where label_id  = #{labelId}")
    int updateLabel(LabelPoReq labelPoReq) throws SQLException;

    /**
     * 删除标签
     * @param deleteLabelPoReq
     * @return
     */
    @Update("update tb_label set is_delete= 1 where label_id  = #{labelId}")
    int deleteLabel(DeleteLabelPoReq deleteLabelPoReq) throws SQLException;

    /**
     * 获取所有的标签
     * @return 标签
     */
    @Select({"select * from tb_label where is_delete = 0"})
    @Results({
            @Result(property = "labelId",column = "label_id"),
            @Result(property = "labelTitle",column = "label_title"),
    })
    List<LabelPoResp> getAllLabels() throws SQLException;

    /**
     * 根据标签名获取标签
     * @param addLabelPoReq
     * @return
     */
    @Select({"select * from tb_label where is_delete = 0 and label_title = #{labelTitle}"})
    @Results({
            @Result(property = "labelId",column = "label_id"),
            @Result(property = "labelTitle",column = "label_title"),
    })
    List<LabelPoResp> getLabelsByTitle(AddLabelPoReq addLabelPoReq) throws SQLException;

    /**
     * 根据标签编号列表获取标签
     *
     * @param labelIdsPoReq
     * @return
     */
    @Select( " <script>" +
            "select * from tb_label where label_id in " +
            " <foreach collection='labelIds' open='(' item='labelId' separator=',' close=')'> #{labelId}</foreach> "+
            " </script>" )
    @Results({
            @Result(property = "labelId",column = "label_id"),
            @Result(property = "labelTitle",column = "label_title"),
    })
    List<LabelPoResp>  getLabelsByLabelIds(LabelIdsPoReq labelIdsPoReq) throws SQLException;


    /**
     * 根据课程编号获取标签编号列表
     * @param courseIdPoReq
     * @return
     */
    @Select({"select label_id from tb_course_label where course_id = #{courseId} and is_delete=0"})
    @Results({
            @Result(property = "labelId",column = "label_id"),
            @Result(property = "labelTitle",column = "label_title"),
    })
    List<String> getLabelIdsByCourseId(CourseIdPoReq courseIdPoReq) throws SQLException;

}
